const { body, validationResult } = require('express-validator');

const registerValidation = [
    body('username').notEmpty().withMessage('用户名不能为空'),
    body('account').notEmpty().withMessage('账号不能为空'),
    body('password').isLength({ min: 6 }).withMessage('密码至少6个字符'),
    body('phone').optional().isMobilePhone('zh-CN').withMessage('无效的手机号码')
];

const loginValidation = [
    body('account').notEmpty().withMessage('账号不能为空'),
    body('password').notEmpty().withMessage('密码不能为空')
];

const updateProfileValidation = [
    body('username').optional().notEmpty().withMessage('用户名不能为空'),
    body('phone').optional().isMobilePhone('zh-CN').withMessage('无效的手机号码'),
    body('avatar').optional().isString().withMessage('头像路径必须是字符串')
];

const validate = (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
    }
    next();
};

module.exports = {
    registerValidation,
    loginValidation,
    updateProfileValidation,
    validate
}; 